﻿Imports System.Runtime.CompilerServices
Imports System.Net
Imports System.Net.Cache
Imports System.Text

Friend Module HttpWebRequestExtention
    ''' <summary>
    ''' 受信したCookieをCookieContainerに格納します。
    ''' </summary>
    ''' <param name="res">Cookieを取得するHttpWebRequest</param>
    ''' <param name="cookies">Cookieを格納するCookieContainer</param>
    ''' <remarks></remarks>
    <Extension()> _
    Public Sub LoadCookies(ByVal res As HttpWebResponse, ByVal cookies As CookieContainer)
        res.Cookies("_twitter_sess").Domain = My.Resources.TwitterDomain
        cookies.Add(res.Cookies)
    End Sub

    ''' <summary>
    ''' TwitterにGETメソッドでアクセスするのに必要なヘッダを設定します。
    ''' </summary>
    ''' <param name="req">ヘッダを設定するHttpWebRequest</param>
    ''' <param name="referer">リファラに設定するURL</param>
    ''' <param name="cookies">Cookieを格納するCookieContainer</param>
    ''' <remarks></remarks>
    <Extension()> _
    Public Sub SetTwitterGetHeaders(ByVal req As HttpWebRequest, ByVal referer As String, ByVal cookies As CookieContainer)
        req.Method = WebRequestMethods.Http.Get
        req.CookieContainer = cookies
        req.UserAgent = "Mozilla/5.0 Twitter Imitation Client.NET"
        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        If referer IsNot Nothing Then
            req.Referer = referer
        End If
        req.CachePolicy = New RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)
        req.KeepAlive = True
        req.AllowAutoRedirect = False
        req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
    End Sub

    ''' <summary>
    ''' TwitterにPOSTメソッドでアクセスするのに必要なヘッダを設定します。
    ''' </summary>
    ''' <param name="req">ヘッダを設定するHttpWebRequest</param>
    ''' <param name="referer">リファラに設定するURL</param>
    ''' <param name="cookies">Cookieを格納するCookieContainer</param>
    ''' <remarks></remarks>
    <Extension()> _
    Public Sub SetTwitterPostHeaders(ByVal req As HttpWebRequest, ByVal referer As String, ByVal cookies As CookieContainer)
        req.SetTwitterGetHeaders(referer, cookies)
        req.Method = WebRequestMethods.Http.Post
        req.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"
    End Sub

    ''' <summary>
    ''' TwitterにXmlHttpRequestとしてGETメソッドでアクセスするのに必要なヘッダを設定します。
    ''' </summary>
    ''' <param name="req">ヘッダを設定するHttpWebRequest</param>
    ''' <param name="referer">リファラに設定するURL</param>
    ''' <param name="cookies">Cookieを格納するCookieContainer</param>
    ''' <remarks></remarks>
    <Extension()> _
    Public Sub SetTwitterXHRGetHeaders(ByVal req As HttpWebRequest, ByVal referer As String, ByVal cookies As CookieContainer)
        req.SetTwitterGetHeaders(referer, cookies)
        req.Accept = "application/json, text/javascript, */*"
        req.Headers.Add("X-Requested-With", "XMLHttpRequest")
    End Sub

    ''' <summary>
    ''' TwitterにXmlHttpRequestとしてPOSTメソッドでアクセスするのに必要なヘッダを設定します。
    ''' </summary>
    ''' <param name="req">ヘッダを設定するHttpWebRequest</param>
    ''' <param name="referer">リファラに設定するURL</param>
    ''' <param name="cookies">Cookieを格納するCookieContainer</param>
    ''' <remarks></remarks>
    <Extension()> _
    Public Sub SetTwitterXHRPostHeaders(ByVal req As HttpWebRequest, ByVal referer As String, ByVal cookies As CookieContainer)
        req.SetTwitterPostHeaders(referer, cookies)
        req.Accept = "application/json, text/javascript, */*"
        req.Headers.Add("X-Requested-With", "XMLHttpRequest")
    End Sub

    ''' <summary>
    ''' 文字列をポストします。
    ''' </summary>
    ''' <param name="req">ポストするHttpWebRequest</param>
    ''' <param name="strPost">ポストする文字列</param>
    ''' <remarks></remarks>
    <Extension()> _
    Public Sub PostString(ByVal req As HttpWebRequest, ByVal strPost As String)
        Dim binPost As Byte() = Encoding.UTF8.GetBytes(strPost)
        req.ContentLength = binPost.Length
        Using sReq = req.GetRequestStream()
            sReq.Write(binPost, 0, binPost.Length)
        End Using
    End Sub
End Module